package com.rd.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rd.dto.Result;
import com.rd.entity.Shop;
import com.rd.service.ShopService;
import com.rd.utils.SystemConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    ShopService shopService;

    /**
     * 根据id查询商铺信息
     * @param id 商铺id
     * @return 商铺详情数据
     */
    @GetMapping("/{id}")
    public Result queryShopById(@PathVariable("id") Long id) {
        return shopService.queryShopById(id);
    }

    @PostMapping
    public Result saveShop(@RequestBody Shop shop) {
        // 写入数据库
        shopService.save(shop);
        // 返回店铺id
        return Result.ok(shop.getId());
    }

    /**
     * 根据商铺类型分页查询商铺信息
     * @param typeId 商铺类型
     * @param current 页码
     * @return 商铺列表
     */

    @GetMapping("/of/type")
    public Result queryShopByType(@RequestParam("typeId") Integer typeId,
                                  @RequestParam(value = "current", defaultValue = "1") Integer current,
                                  @RequestParam(value = "x",required = false) Double x,
                                  @RequestParam(value = "y",required = false) Double y){

        return shopService.queryShopByType(typeId,current,x,y);

    }

    /**
     * 根据商铺名称关键字分页查询商铺信息
     * @param name 商铺名称关键字
     * @param current 页码
     * @return 商铺列表
     */
    @GetMapping("/of/name")
    public Result queryShopByName(@RequestParam("name") String name,
                                  @RequestParam(value = "current", defaultValue = "1") Integer current){
        Page page = new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE);
        // 模糊查询，like(boolean condition,String column,Object name)
        // 符合条件才拼接sql
        shopService.query().like(StrUtil.isNotBlank(name),"name",name).page(page);
        return Result.ok(page.getRecords());
    }

    @PutMapping
    public Result updateShop(@RequestBody Shop shop){
        return shopService.updateShop(shop);
    }
}
